﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace WebCamCapture
{
    public partial class CatureForm : Form
    {
        enum CameraState
        {
            Off,
            On
        }

        CaptureDevice _Camera;
        CameraState _CameraState = CameraState.Off;
        int _SnapShotCount = 1;

        public CatureForm(CaptureDevice camera) : this()
        {
            _Camera = camera;

            if(null==_Camera)
            {
                throw(new ArgumentException("camera can not be null"));
            }

            ToggleCamera(CameraState.On);
        }
        public CatureForm()
        {
            InitializeComponent();
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            ToggleCamera(CameraState.Off);
            base.OnClosing(e);
        }
        //this works on to max but not from max
        //protected override void OnResize(EventArgs e)
        //{
        //    base.OnResize(e);

        //    if (this.WindowState == FormWindowState.Maximized)
        //    {
        //        _Camera.Resize(pbImage);
        //    }
        //    else if (this.WindowState == FormWindowState.Minimized)
        //    {
        //        ToggleCamera(CameraState.Off);
        //    }
        //}
        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResizeEnd(e);
            _Camera.Resize(pbImage);
        }
        protected override void OnMaximizedBoundsChanged(EventArgs e)
        {
            base.OnMaximizedBoundsChanged(e);
        }

        private void ToggleCamera(CameraState value)
        {
            if (_CameraState == value) return;

            _CameraState = value;

            if (CameraState.On == _CameraState)
            {
                startButton.Visible = false;
                stopButton.Enabled = false;
                pbImage.Visible = stopButton.Visible = true;
                Refresh();
                _Camera.Attach(pbImage);
                snapshotButton.Enabled = stopButton.Enabled = true;
            }
            else
            {
                startButton.Enabled = false;
                startButton.Visible = true;
                pbImage.Visible = snapshotButton.Enabled = stopButton.Visible = false;
                Refresh();
                _Camera.Detach();
                startButton.Enabled = true;
            }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            ToggleCamera(CameraState.On);
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            ToggleCamera(CameraState.Off);
        }

        private void snapshotButton_Click(object sender, EventArgs e)
        {
            try
            {
                Image image = _Camera.Capture();
                image.Save(Path.Combine(Application.StartupPath, string.Format("Capture_{0}.png", _SnapShotCount)), ImageFormat.Png);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            ++_SnapShotCount;
        }
    }
}
